package com.cskaoyan.processor;

import com.cskaoyan.bean.LifeCycleBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author stone
 * @date 2022/09/30 10:10
 */
public class CustomBeanPostProcessor implements BeanPostProcessor {
    // 正在执行生命周期的组件的实例（执行了BeanPostProcessor的postProcessBeforeInitialization之前的）作为形参传入
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        // username 由 xuejia变为雪茄
        // 筛选LifeCycleBean
        // 可以用beanName也可以用类型做筛选,用类型会多一些
        if ("lifecycleBean".equals(beanName) && bean instanceof LifeCycleBean) {
            ((LifeCycleBean)bean).setUsername("雪茄");
        }

        System.out.println("BeanPostProcessor的postProcessBeforeInitialization:" + beanName);
        return bean;//返回值是你最终放入到容器中的组件
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor的postProcessAfterInitialization:" + beanName);
        return bean;
    }
}
